/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.reader.data.net;

import java.io.BufferedReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class BaseHttp {
	private final int CONNECTION_TIMEOUT = 15000;
	private final int SO_TIMEOUT = 30000;
	private String URL = "";
	private UrlEncodedFormEntity entity = null;
	private HttpClient httpClient = null;
	private HttpGet httpGet = null;
	private HttpPost httpPost = null;
	private BufferedReader in = null;
	private HttpResponse response = null;

	public BaseHttp(String paramString) {
		this.URL = paramString;
	}

	public BaseHttp(String paramString,
			UrlEncodedFormEntity paramUrlEncodedFormEntity) {
		this.URL = paramString;
		this.entity = paramUrlEncodedFormEntity;
	}

	private void initHttpClient() throws Exception {
		this.httpClient = new DefaultHttpClient();
		this.httpClient.getParams().setParameter("http.connection.timeout",
				Integer.valueOf(15000));
		this.httpClient.getParams().setParameter("http.socket.timeout",
				Integer.valueOf(30000));
	}

	private String readResponse() throws Exception {
		return EntityUtils.toString(this.response.getEntity());
	}

	public String doGet() throws Exception {
		this.httpGet = new HttpGet(this.URL);
		initHttpClient();
		this.response = this.httpClient.execute(this.httpGet);
		return readResponse();
	}

	public String doPost() throws Exception {
		initHttpClient();
		this.httpPost = new HttpPost(this.URL);
		this.httpPost.setEntity(this.entity);
		this.response = this.httpClient.execute(this.httpPost);
		return readResponse();
	}
}